<?php
/**
 * Description: 表达改写模块
 * User: devforma
 * Date: 16/5/28
 * Time: 10:10
 */
require_once __DIR__ . '/ProcessExerciseBase.class.php';

class BiaodagaixieModule extends ProcessExerciseBase {

    /**
     * 设置作业类型
     */
    protected function setExerciseType() {
        $this->data['exercise_type'] = 5;
    }

    /**
     * 设置作业题目内容
     */
    protected function setExerciseQuestion() {
        global $cmsdb;

        //获取所有题目
        $questionStr = implode(',', $this->questionIds);
        $sql = "SELECT * FROM `original_primary_question` WHERE `id` IN ({$questionStr}) ORDER BY FIELD (`id`, {$questionStr})";
        $questionInfo = (array)$cmsdb->getResults($sql);

        $this->data['question'] = [];
        foreach ($questionInfo as $q) {
            $this->data['question'][] = array(
                'question_id'     => (int)$q->id,
                'question'        => html2text($q->content),
                'answer'          => html2text($q->right_answer),
                'recomend_answer' => empty($q->recommend_answer) ? [] : $this->getQuestionRecommendAnswer($q->id, explode('#', $q->recommend_answer))
             );
        }
    }

    /**
     * 设置提交权限
     */
    protected function setSubmitPriv() {
        parent::setSubmitPriv();
    }

    /**
     * 设置用户答案
     * 
     */
    protected function setUserAnswer() {
        global $uddb;

        //按照题序获取答题记录
        $questionOrder = implode(',', $this->questionIds);
        $sql = "SELECT * FROM `history_user_primary_question` WHERE `exercise_id`=:eid AND `user_id`=:uid ORDER BY FIELD (`question_id`, {$questionOrder})";
        $questionRecords = (array)$uddb->getResults($sql, array(':eid' => $this->exerciseId, ':uid' => $this->userId));

        $this->data['user_answer'] = array();
        if (!empty($questionRecords)) {
            foreach ($questionRecords as $q) {
                $this->data['user_answer'][$q->question_id] = array(
                    'question_id' => $q->question_id,
                    'action_time' => $q->acting_time,
                    'answer'      => $q->user_answer
                );
            }

            //需要题目与答案的合并
            $this->shouldMergeQA = true;
        }
    }

    /**
     * 校验参数
     *
     * @param array $args 参数
     * @return bool
     */
    public function validateArgs($args) {
        if (!empty($args['answer_content']) && !empty($args['question_id'])) { //多个关键词拼接的字符串
            $this->submitArgs = $args;
            return true;
        }

        return false;
    }

    /**
     * 提交作业的额外操作
     */
    protected function doExtra() {}

    /**
     * 设置作业记录的状态
     */
    protected function setSubmitStatus() {
        if (empty($this->submitArgs['done'])) {
            $this->submitExerciseRecord['status'] = 0; //未完成
        } else {
            if ($this->exerciseInfo->end_time > $this->now)
                $this->submitExerciseRecord['status'] = 1; //已完成
            else
                $this->submitExerciseRecord['status'] = 5; //过期完成
        }
    }

    /**
     * 保存答题记录记作业统计信息
     */
    protected function saveQuestionRecordsAndSetSubmitSummary() {
        global $uddb;

        //是否之前提交过了
        $exists = $uddb->getVar('SELECT `id` FROM `history_user_primary_question` WHERE `user_id`=:uid AND `exercise_id`=:eid AND `question_id`=:qid',
            array(':uid' => $this->userId, ':eid' => $this->exerciseId, ':qid' => $this->submitArgs['question_id']));

        //构造答题记录
        $record = array(
            'question_id' => $this->submitArgs['question_id'],
            'exercise_id' => $this->exerciseId,
            'user_id'     => $this->userId,
            'acting_time' => $this->now,
            'user_answer' => $this->submitArgs['answer_content'],
            'is_right'    => 1,
            'status'      => 1
        );

        //更新or新增
        if ($exists) {
            $uddb->update('history_user_primary_question', $record, array('id' => $exists));
        } else {
            $uddb->insert('history_user_primary_question', $record);
        }

        $this->submitExerciseRecord['summary'] = '';
    }

}